(function(){
	$.extend($.fn,{
		categoryFloatPanel: function() {
			var $that_ = $(this);
			var options_ = {
				type: 1,
				deep: 1,
				onSelect: function(treeId, treeNode){}
			}
			var containerId_ = "selectCat" + $that_.attr("id");
			var $container_ = $("#" + containerId_); 
			var loadPath_ = g_domain + "/category/listWithSons/json";
			var zTree_ = null;
			var zTreeSetting_ = {
				data : {
					simpleData : {
						enable : true,
						idKey : "catId",
						pIdKey : "parentId",
						rootPId : null
					}
				},
				callback : {
					onClick : function (event, treeId, treeNode){
						options_.onSelect(treeId, treeNode);
						$container_.hide();
					},
					onExpand: function (event, treeId, treeNode) {
						if (!treeNode.children) {
							loadData_(treeNode);
						}
					}
				}
		
			};
			
			var loadData_ = function(parentNode){
				var vars = {
					deep: options_.deep,
					type: options_.type
				};
				if(parentNode){
					vars = {
						"category.catId":parentNode.catId
					};
				}
				$.getJSON(loadPath_,vars,function(data){
					if (data.status != 0) {
						alert(data.message);
						return;
					}
					var nodes = [];
					var list = data.result;
					if (!list) {
						return;
					}
					var length = list.length;
					for(var i = 0; i < length; i++ ){
						var node = list[i];
						// 这句的目的是使节点图标默认为文件夹图标
						node.isParent = true;
						node.open = true;
						nodes.push(node);
					}
					zTree_.addNodes(parentNode, nodes);
					
				});
			}
			
			var makeContent_ = function() {
				$that_.after('<div id="' + containerId_ + '" class="ztree" style="clear:both;background-color: #F7F7F7;border:1px solid #C7C6C6;width:auto;min-width:200px;max-width:400px;height:auto;min-height:100px;max-height:400px;position:absolute;padding:5px;z-index:1;margin:0px;display:none;"><div>');
				$container_ = $("#" + containerId_);
				zTree_ = $.fn.zTree.init($container_, zTreeSetting_);
				loadData_();
			}
			
			if (arguments.length > 0) {
				var options = arguments[0];
				for (var key in options) {
					if (options_[key] != undefined) {
						options_[key] = options[key];
					}
				}
			}
			
			$that_.parent().hover(function() {
				$container_.hide();
			});
			$that_.off("click");
			$that_.click(function() {
				if ($container_.length == 0) {
					makeContent_();
				} 
				$container_.show();
			});
			
		}
	})
})();

(function(){
	$.extend($.fn,{
		categoryComboBox: function() {
			var $that_ = $(this);
			var options_ = {
				deep: 1,
				type: 0,
				value: 0,
				isParentValue: false,
				nullText: "",
				isLoadParent: true,
				isShowNull: true,
				isShowId: false,
				isReloadSons: false,
				onLoad: function($select){},
				onSelect: function(deep, category){}
			};
			var cacheData_ = {};
			var loadRootsPath_ = g_domain + "/category/listTypeRoots/json";
			var loadSonsPath_ = g_domain + "/category/listSons/json";
			var loadBrothersPath_ = g_domain + "/category/listBrothers/json";
			var containerId_ = "selectCat_" + $that_.attr("id") + "_";	
			var loadBrothers_ = function(catId, isPrepend){
				vars = {
					"catId":catId
				};
				$.getJSON(loadBrothersPath_ + "?r=" + Math.random(), vars, function(data){
					makeSelectHtml_(data, catId, isPrepend);
				});
			};
			var loadSons_ = function(catId, value){
				var loadPath = loadRootsPath_;
				var vars = {
					type:options_.type
				};
				if (catId) {
					loadPath = loadSonsPath_;
					vars = {
						"catId":catId
					};
				}
				$.getJSON(loadPath + "?r=" + Math.random(), vars, function(data){
					makeSelectHtml_(data, value);
				});
			};
			var makeSelectHtml_  = function(data, value, isPrepend) {
				if (data.status != 0) {
					alert(data.message);
					return;
				}
				var parent = data.result;
				var list = parent.categories;
				if (!list || list.length == 0) {
					return;
				}
				var deep = parseInt(list[0].deep);
				var selectTagId = containerId_ + deep;
				var $select = $("#" + selectTagId);
				if ($select.length == 0) {
					if (isPrepend) {
						$that_.prepend('<select id="' + selectTagId + '"></select>');
					} else {
						$that_.append('<select id="' + selectTagId + '"></select>');
					}
					$select = $("#" + selectTagId);
					
				} else {
					$select.html("");
				}
				
				if (options_.isShowNull) {
					if (options_.nullText) {
						$select.append("<option value=''>" + options_.nullText + "</option>");
					} else {
						$select.append("<option value=''>-类别" + deep + "-</option>");
					}
					
				}
				for (var i = 0; i < list.length; i++) {
					cacheData_[list[i].catId] = list[i];
					var text = list[i].name + (options_.isShowId?"【" + list[i].catId +"】":"");
					$select.append("<option value='" + list[i].catId + "'>" + text + "</option>");
				}
				
				// 当前列表有选中值,但下一级没有加载,则加载下一级
				if (value && ($("#" + containerId_ + (deep + 1)).length == 0 || options_.isReloadSons) ) {
					loadSons_(value);
				}
				
				if (options_.isLoadParent && parent && parent.deep >= options_.deep && $("#" + containerId_ + (deep - 1)).length == 0) {
					loadBrothers_(parent.catId, true);
				}
				// 选中值
				$select.val(value);
				options_.onSelect(deep, cacheData_[value]);
				options_.onLoad($select);
			};
			
			if (arguments.length > 0) {
				var options = arguments[0];
				for (var key in options) {
					if (options_[key] != undefined) {
						options_[key] = options[key];
					}
				}
			}
			
			$(document).off("change", "[id^='" + containerId_ + "']");
			$(document).on("change", "[id^='" + containerId_ + "']", function() {
				var value = $(this).val();
				var idSplits = $(this).attr("id").split("_");
				var deep = parseInt(idSplits[idSplits.length -1]);
				$("[id^='" + containerId_ + "']").each(function(i, el) {
					idSplits = $(this).attr("id").split("_");
					var deep2 = parseInt(idSplits[idSplits.length -1]);
					if (deep2 > deep) {
						options_.onSelect(deep2);
						$(this).remove();
					}
				});
				
				options_.onSelect(deep, cacheData_[value]);
				
				if (value) {
					loadSons_(value);
				}
			});
			
			if (options_.value) {
				if (options_.isParentValue) {
					loadSons_(options_.value);
				} else {
					loadBrothers_(options_.value);
				}
			} else {
				loadSons_();
			}
		}
	});
})();